// handle ajax answer
var phpmyhandleSuccess = function(o){
    var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1);    
    var data = eval('(' + json + ')');   
    var timer = 5; // time until hide
    if (data.Results.MSGBody) {
        // show Panel
        var pan = new yuiPanel;
        pan.setBody(data.Results.MSGBody);
        if (data.Results.MSGHead) 
            pan.setHead(data.Results.MSGHead);
        if (data.Results.MSGFoot) 
            pan.setFoot(data.Results.MSGFoot);        
        pan.show();
        timer = parseInt((data.Results.MSGTime ? data.Results.MSGTime : timer) * 1000)
        setTimeout(function(){
            pan.hide();
        }, timer);
    }
    if (data.Results.replaceContentById) {
        // some field values to replace
        for (var Key in data.Results.replaceContentById) {
            if (!Key) 
                continue;
            var el = YAHOO.util.Dom.get(Key);
            if (el) {
                el.innerHTML = data.Results.replaceContentById[Key];
            }
            delete el;
        }
    }
    if (data.Results.UpdateValue) {
        // some field values to replace
        for (var Key in data.Results.UpdateValue) {
            if (!Key) 
                continue;
            var el = YAHOO.util.Dom.get(Key);
            if (el) 
                el.value = data.Results.UpdateValue[Key];
            delete el;
        }
    }
    if (data.Results.js) 
        eval(data.Results.js);
}
// handles ajax fail
var phpmyhandleFailure = function(o){
    alert('XHR-Request failed!');
}
// default call back handler for ajax requests
var phpmycallback = {
    success: phpmyhandleSuccess,
    failure: phpmyhandleFailure
};
/**
 * easy form ajax sender
 * e.g.: stepforminit = new ajaxFormPost({buttonid: 'submitStep', formid: 'stepform', target: '?p=ajax&s=updatestep'});
 * 
 * @param object buttonid = button to start submission, formid = form to get datas from, target = xhr target
 * 
 */
var ajaxFormPost =  function (vars) {            
        var buttonid,formid,target,init = false;
        var obj = this;
        if (vars.buttonid)  
            this.buttonid = vars.buttonid;  
        if (vars.formid)  
            this.formid = vars.formid;
        if (vars.target)  
            this.target = vars.target;            
        {
            if (this.init) {                
                return true;
            }            
            this.init = true;
            var _button = new YAHOO.widget.Button(this.buttonid);
            _button.on('click', function () { obj.clicked(); });
        }    
        this.clicked = function () { _ajaxSendPostForm(this.target, this.formid);}            
}
/**
 * send a form via xhr post
 * @param string xhr target
 * @param string form id to send
 * @param sring datas to add to post submission
 */
var _ajaxSendPostForm = function(target, formid, data){                
                var sUrl = target;                
                YAHOO.util.Connect.setForm(formid);
                var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, phpmycallback, data);
}
 /**
  * send a xhr post
  * @param string xhr target
  * @param sring datas to add to post submission
  */
 var _ajaxSendPost = function(target, data){                
                 var sUrl = target;                
                 var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, phpmycallback, data);
 }
var YDom = YAHOO.util.Dom, YEvent = YAHOO.util.Event, status = null;
